<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        let c = 0
        function process(i) {
            c++
            return i + 1
        }

        const array = new Array(10000000);




        console.time("for")
        for (let i = 0; i < array.length; i++) {
            process(array[i])
        }
        console.timeEnd("for")          // for: 0.0458984375 ms


        // better 
        console.time("better for")
        for (let i = 0, length = array.length; i < length; i++) {
            process(array[i])
        }
        console.timeEnd("better for")   //  better for: 0.01611328125 ms


        // 达夫设备优化循环
        console.time("达夫设备")

        let iterations = Math.floor(array.length / 16)
        let leftover = array.length % 16
        let i = 0
        if (leftover > 0) {
            do {
                process(array[i++])
            } while (--leftover > 0)
        }
        do {
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])

            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
            process(array[i++])
        } while (--iterations > 0)
        console.timeEnd("达夫设备")


        function quickLoopForHugeData16(process, array, loopCount) {
            let iterations = Math.floor(loopCount / 16)
            let leftover = loopCount % 16
            let i = 0
            if (leftover > 0) {
                do {
                    process(array[i++])
                } while (--leftover > 0)
            }
            do {
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
                process(array[i++])
            } while (--iterations > 0)

        }

    </script>
</body>

</html>